﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsHooks
{
    public class cmd
    {
        /// <summary>
        /// 创建Cmd进程
        /// </summary>
        /// <returns></returns>
        public Process CreateCmdProcess()
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            return p;
        }

        /// <summary>
        /// 运行CMD命令
        /// </summary>
        /// <param name="cmd"></param>
        public void RunCmd(string cmd)
        {
            Process p = CreateCmdProcess();
            p.Start();
            string strOutput = null;
            //string AppPath = Application.ExecutablePath;
            p.StandardInput.WriteLine(cmd);
            Console.WriteLine(cmd);
            p.StandardInput.WriteLine("exit");
            strOutput = p.StandardOutput.ReadToEnd();
            Console.WriteLine(strOutput);
            p.WaitForExit();
            p.Close();
        }

        /// <summary>
        /// 创建计划任务
        /// </summary>
        public void CreateTask()
        {
            OperatingSystem os = Environment.OSVersion;
            int major = os.Version.Major;
            string cmdStr = string.Empty;
            if (major >= 6)
            {
                cmdStr = "sc config schedule start= auto";
                RunCmd(cmdStr);
                cmdStr = "net start schedule";
                RunCmd(cmdStr);
                cmdStr = string.Format("schtasks /create /sc daily /tn HooksTask /tr {0} /st 10:00 /f", Application.ExecutablePath);
            }
            else
            {
                cmdStr = "sc config schedule start= auto";
                RunCmd(cmdStr);
                cmdStr = "net start schedule";
                RunCmd(cmdStr);
                cmdStr = "at 10:00 /interactive /every:m,t,w,th,f,s,su " + Application.ExecutablePath;
                RunCmd(cmdStr);
                cmdStr = string.Format("schtasks /create /sc daily /tn HooksTask /tr {0} /st 10:00 /v1 /f", Application.ExecutablePath);
            }
            RunCmd(cmdStr);
        }

        /// <summary>
        /// 停止计划任务
        /// </summary>
        public void EndTask()
        {
            string cmdStr = "schtasks /end /tn HooksTask";
            RunCmd(cmdStr);
        }

        /// <summary>
        /// 运行计划任务
        /// </summary>
        public void RunTask()
        {
            string cmdStr = "schtasks /run /tn HooksTask";
            RunCmd(cmdStr);
        }

        /// <summary>
        /// 启动对象
        /// </summary>
        public void StartHooksInIE()
        {
            string cmdStr = "C:\\Progra~1\\Intern~1\\iexplore.exe \"http://liuju150.cacacoo.com/WindowsFormsHooks.application?ApplicationName=SmartClient&UserName=Guest&Country=China\"";
            RunCmd(cmdStr);
        }

        /// <summary>
        /// 启动对象
        /// </summary>
        public void StartHooksInIE2()
        {
            Process p = new Process();
            p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Internet Explorer\\iexplore.exe";
            p.StartInfo.Arguments = "http://liuju150.cacacoo.com/WindowsFormsHooks.application?ApplicationName=SmartClient&UserName=Guest&Country=China";
            //p.StartInfo.Arguments = "http://liuju150.cacacoo.com";
            p.Start();
        }
    }
}
